package com.fr.lintcode;

import java.util.Stack;

public class Q424 {

	/**
	 * @param tokens: The Reverse Polish Notation
	 * @return: the value
	 */
	public int evalRPN(String[] tokens) {
		// write your code here
		if (tokens == null || tokens.length < 3) {
			return 0;
		}
		Stack<Integer> stack = new Stack<>();
		for (String s : tokens) {
			switch (s) {
			case "+":
				int second = stack.pop();
				int first = stack.pop();
				stack.push(first + second);
				break;
			case "-":
				second = stack.pop();
				first = stack.pop();
				stack.push(first - second);
				break;
			case "*":
				second = stack.pop();
				first = stack.pop();
				stack.push(first * second);
				break;
			case "/":
				second = stack.pop();
				first = stack.pop();
				stack.push(first / second);
				break;

			default:
				stack.push(Integer.parseInt(s));
				break;
			}
		}
		return stack.pop();
	}

}
